<template>
  <!--
  单纯页面修改功能：
    1：定义修改接口api
    2:导入接口api
    3:在表单验证成功后进入api调用（表单验证处理）
    4：调用成功
       提示
       网站中所有使用该数据的地方都要更新

 -->

  <div class="detail">
    <el-card>
      {{ userinfo }}
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form
            ref="form"
            label-width="100px"
            class="form"
            :model="form"
            :rules="rules"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" class="setw" />
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input v-model="form.password" type="password" class="setw" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane
          label="个人详情"
          lazy
        ><Info
          :user-info="initUserinfo"
          @getData="getData"
        /></el-tab-pane>
        <el-tab-pane label="岗位信息" lazy>
          <Job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser } from '@/api/user'
import { sysUserPut } from '@/api/employees'

export default {
  components: {
    Info: () => import('./components/Info.vue'),
    Job: () => import('./components/Job.vue')
  },
  data() {
    return {
      userId: this.$route.params.id,
      form: {
        username: '', //	string	非必须
        password: '' // string	非必须
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 2,
            max: 16,
            message: '请输入2-16位姓名',
            trigger: 'change'
          }
        ], //	string	非必须
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 16,
            message: '请输入6-16位密码',
            trigger: 'change'
          }
        ] // string	非必须
      },
      userinfo: {},
      initUserinfo: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.userId)
      this.userinfo = res.data
      this.initUserinfo = JSON.parse(JSON.stringify(res.data))
      this.form.username = res.data.username
      console.log('用户信息', res)
    },
    async submit() {
      await this.$refs.form.validate()
      await sysUserPut({ ...this.userinfo, ...this.form })
      this.$message.success('更新成功')
      // 更新当前登录帐号用户信息（如果当前修改用户就是当前登录帐号）
      if (this.userId === this.$store.state.user.userInfo.id) {
        this.$store.dispatch('user/getUserInfo')
      }
      // 更新子组件内的用户信息
      this.getData()
      // this.initUserinfo.username = this.form.username
    }
  }
}
</script>
<style lang="scss" scoped>
.detail {
  padding: 20px;
  .form {
    margin-left: 100px;
  }
  .setw {
    width: 300px;
  }
}
</style>
